package com.wei.test.Util;

import com.wei.test.Service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.*;
import java.time.format.DateTimeFormatter;

@Component
public class DateTimeUtil {
    private static DateTimeFormatter defaultFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private static DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private static DateTimeFormatter timeFormat = DateTimeFormatter.ofPattern("HH:mm:ss");

    @Autowired
    private LogService log;

    public String date() {
        LocalDate today = LocalDate.now();
        return today.toString();
    }

    public LocalDate date(String dateStr) {
        return LocalDate.parse(dateStr, dateFormat);
    }

    public String time() {
        LocalTime nowTime = LocalTime.now().withNano(0);
        return nowTime.toString();
    }

    public LocalTime time(String timeStr) {
        return LocalTime.parse(timeStr, timeFormat);
    }

    public String dateTime() {
        String dateTimeStr = LocalDateTime.now().withNano(0).format(defaultFormat);
        return dateTimeStr;
    }

    public LocalDateTime dateTime(String dtStr) {
        dtStr = unifyDateTime(dtStr);
        return LocalDateTime.parse(dtStr, defaultFormat);
    }

    private String unifyDateTime(String dt) {
        if (dt.length() == 10) {
            dt = dt + " 00:00:00";
        } else if (dt.length() == 8) {
            dt = date() + " " + dt;
        }

        return dt;
    }

    public Duration betweenDateTime(String dt, String dt2) {
        if (dt.length() != dt2.length()) {
            log.error("时间格式不统一");
            return null;
        }

        dt = unifyDateTime(dt);
        dt2 = unifyDateTime(dt2);
        Duration duration = Duration.between(dateTime(dt), dateTime(dt2));
        return duration;
    }

    public Duration betweenDateTime(String dt) {
        dt = unifyDateTime(dt);
        Duration duration = Duration.between(dateTime(dt), LocalDateTime.now());
        return duration;
    }

    public String plusMinutes(int amount) {
        LocalDateTime now = LocalDateTime.now().plusMinutes(amount).withNano(0);
        return now.format(defaultFormat);
    }

    public String plusMinutes(String dtStr, int amount) {
        dtStr = unifyDateTime(dtStr);
        LocalDateTime dt = dateTime(dtStr);
        return dt.plusMinutes(amount).withNano(0).format(defaultFormat);
    }

    public long timestamp() {
        return LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    public long timestamp(String dtStr) {
        LocalDateTime dt = dateTime(dtStr);
        return dt.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    public static void main(String[] args) {
        DateTimeUtil d = new DateTimeUtil();
        String a = d.date();
        String b = d.time();
        String c = d.dateTime();
        LocalDate a1 = d.date("2019-05-20");
        LocalDate a2 = d.date("2019-06-21");
        Duration d1 = d.betweenDateTime("2019-05-20");
//        System.out.println(d1.toDays());
//        System.out.println(d1.toMinutes());
//        System.out.println(d1.toHours());
//        System.out.println(d1.toMillis());
        System.out.println(d.plusMinutes(1));
        System.out.println(d.plusMinutes("2019-06-02", 100));
        System.out.println(d.timestamp("2019-05-31"));
        System.out.println(d.timestamp("2019-06-01"));
        System.out.println(d.timestamp("2019-06-02"));
        LocalTime b1 = d.time("23:18:01");
        LocalDateTime c1 = d.dateTime("2019-06-20 00:21:22");
//        System.out.println(a1);
//        System.out.println(b1);
//        System.out.println(c1);
    }

}
